# import python_aida64
# from pprint import pprint

# # pprint(python_aida64.getData())
# print("---------")
# # pprint(python_aida64.getXmlRawData())
# python_aida64.getXmlRawData()

# # root = python_aida64._readRawData().getroot()
# # print(root)

# options = [100 * i for i in range(20, 100)]
# print(options)

import mmap
import sys
from xml.etree import ElementTree as ET

def _readRawData(length):
    with mmap.mmap(
            -1, length,  # anonymous file
            tagname='AIDA64_SensorValues',
            access=mmap.ACCESS_READ) as mm:
        return mm.read()


def _decode(b):
    for encoding in (sys.getdefaultencoding(), 'utf-8', 'gbk'):
        try:
            return b.decode(encoding=encoding)
        except UnicodeDecodeError:
            continue
    return b.decode()

def getXmlRawData() -> str:
    options = [100 * i for i in range(20, 100)]  # ranges in [2k, 10k]
    low = 0
    high = len(options) - 1
    while low < high:  # [low, high], stops at [low, low]
        mid = (low + high) // 2  # legit
        try:
            length = options[mid]
            # print("the len is %d\r\n" %(length))
            # print("run here\r\n")
            raw = _readRawData(length)
            if raw[-1] == 0:  # legit ending
                decoded = _decode(raw.rstrip(b'\x00'))
                return '<root>{}</root>'.format(decoded)
            else:  # not long enough
                print("not long enough\r\n")
                low = mid
                continue
        except PermissionError:  # illegal length (too long)
            high = mid
            continue


# print(root.tag)
# for child_of_root in root:
# 	print(child_of_root.tag) #child_of_root.attrib
# 	# print(child_of_root.attrib)

# for elem in root.iter():
# 	# print elem.tag, elem.attrib
# 	print(elem.text)

# for elem in root.iter():
# 	# print(elem.tag)
# 	if elem.tag == 'sys':
# 		for child_elem in elem:
# 			print(child_elem.tag)

	# for elem in child_of_root.iter():
	# 	# print(child_of_root.tag)
		# print(elem.tag)


# for elem in root.iter():
# 	if elem.text == 'CPU Clock':
# 		continue
	# break_num = 0
	# if break_num != 0:
	# 	break_num--
	# 	continue
	# else:
	# 	if elem.text == 'SCPUCLK':
	# 		print(new_elem.text)
	# 		break_num = 1
	# 		continue


# def get_root_iter_length(root):
# 	i = 0
# 	for elem in root.iter():
# 		i+=1
# 	return i


# i = 0
# while i < get_root_iter_length(root):
# 	if root.iter()[i].text == 'SCPUCLK':
# 		print(root.iter()[i].text)
# print(get_root_iter_length(root))

class Aida64_Basic_Data(object):
	def __init__(self):
		self.__clear_data(self)

	def __clear_data(self, para):
		self.CPUCLCK = '0'
		self.CPUUTI = '0'
		self.MEMUTI = '0'
		self.GPU1UTI = '0'
		self.RTSSFPS = '0'
		self.CPUTEMP = '0'
		self.GPUTEMP = '0'

	def get_datas(self):
		break_num = 0
		last_elem_text = 'null'
		root = ET.fromstring(getXmlRawData())
		for elem in root.iter():
			
			if break_num > 0:
				# do something
				if break_num == 2:
					break_num -= 1
					continue
				else:
					if last_elem_text == 'SCPUCLK':
						self.CPUCLCK = elem.text
					elif last_elem_text == 'SCPUUTI':
						self.CPUUTI = elem.text
					elif last_elem_text == 'SMEMUTI':
						self.MEMUTI = elem.text
					elif last_elem_text == 'SGPU1UTI':
						self.GPU1UTI = elem.text
					elif last_elem_text == 'SRTSSFPS':
						self.RTSSFPS = elem.text
					elif last_elem_text == 'TCPU':
						self.CPUTEMP = elem.text
					elif last_elem_text == 'TGPU1DIO':
						self.GPUTEMP = elem.text
					# print(elem.text)
					break_num = 0
					last_elem_text = 'null'
			else:
				if elem.text == 'SCPUCLK' or elem.text == 'SCPUUTI' or elem.text == 'SMEMUTI' or elem.text == 'SGPU1UTI' or elem.text == 'SRTSSFPS' or elem.text == 'TCPU' or elem.text == 'TGPU1DIO':
					last_elem_text = elem.text
					break_num = 2
					continue





if __name__ == '__main__':
	print("Testing this module...")
	test = Aida64_Basic_Data()
	test.get_datas()
	print(test.RTSSFPS)
	# fund_result = get_fund_by_code_num("000001")
	# print(test.fund_code)
	# print(test.fund_estimated_date)